#ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
-> PS_head

%ps_include: begin
save
/ed {exch def} def

# redefine dangerous operators
{} /showpage ed
{} /copypage ed
{} /erasepage ed
{} /letter ed

# computations are done in the context of a new dictionary
36 dict dup /PS-include-dict-dw ed begin

# context holds the save object created earlier
/context ed

# save and clear the operand stack
count array astore /o-stack ed

# the following variables are now expected:
#	llx,lly,urx,ury	bounding box of picture to be included
#	w		nonzero if space should be painted white to start
#	o		nonzero if space should be outlined
#	s		nonzero if both dimensions should be scaled
#	cx,cy		center of page space in current coordinates
#	sx,sy		size of page space in current coordinates
#	ax,ay		left-right, up-down adjustment of picture in page space
#	rot		rotation of picture in page space
%ps_include: variables begin



-> PS_setup

%ps_include: variables end

# some routines:
# - BBOX llx lly urx ury	put bounding box on stack
# llx lly urx ury BOXPATH -	make a path with given box corners
# dx dy LEN length		compute length of positionless vector
# a b MIN min			compute minimum of two numbers
# a b MAX max			compute maximum of two numbers
# x y NICE x y			move to pixel boundaries in default coords
{llx lly urx ury} /bbox ed
{newpath 2 index exch 2 index exch dup 6 index exch
 moveto 3 {lineto} repeat closepath} /boxpath ed
{dup mul exch dup mul add sqrt} /len ed
{2 copy gt {exch} if pop} /min ed
{2 copy lt {exch} if pop} /max ed
{transform round exch round exch A itransform} /nice ed

# A is the transformation from default to current coordinates
{6 array} /n ed
n defaultmatrix n currentmatrix n invertmatrix n concatmatrix /A ed

# Sx,Sy and Cx,Cy are dimensions and size of bounding box in current coordinates
urx llx sub 0 A dtransform len /Sx ed
0 ury lly sub A dtransform len /Sy ed
llx urx add 2 div lly ury add 2 div A transform /Cy ed /Cx ed

# H and W are height and width of rotated box in current coordinates
rot dup sin abs /S ed cos abs /C ed
Sx S mul Sy C mul add /H ed
Sx C mul Sy S mul add /W ed

# Scalex and Scaley are the required horizontal and vertical scaling factors
sy H div /Scaley ed
sx W div /Scalex ed

# Preserve aspect ratio if we're not scaling both dimensions (ie. s is 0)
s 0 eq {Scalex Scaley min dup /Scalex ed /Scaley ed} if

# add to cx,cy the shift needed within the page space
sx Scalex W mul sub 0 max ax 0.5 sub mul cx add /cx ed
sy Scaley H mul sub 0 max ay 0.5 sub mul cy add /cy ed

# the actual rotation needed is rot less the current rotation
urx llx sub 0 A dtransform exch atan rot exch sub /rot ed

# set up the coordinate system
n currentmatrix initgraphics setmatrix
cx cy translate
Scalex Scaley scale
rot rotate
Cx neg Cy neg translate
A concat

# set the clipping region, and conditionally whiteout and outline
bbox boxpath clip newpath
w 0 ne {gsave bbox boxpath 1 setgray fill grestore} if

# pop local dictionary from the dict stack
end

# now begins the actual material extracted from the file
gsave
%ps_include: inclusion begin



-> PS_tail

%ps_include: inclusion end
grestore

# within the context of the local dictionary ...
PS-include-dict-dw begin

o 0 ne {gsave A defaultmatrix /A ed llx lly nice urx ury nice
	initgraphics 0.1 setlinewidth boxpath stroke grestore} if

# ... restore the operand stack and the save context
clear o-stack aload pop
context end restore
%ps_include: end
